import java.util.ArrayList;
import java.util.List;

/**
 * @program: LeetCode
 * @description: LeetCode : 1002. 查找共用字符
 * @author: WXY
 * @create: 2023-02-08 17:24
 * @Version 1.0
 **/
public class Num1002_commonChars {
    public static List<String> commonChars(String[] words) {
        List<String> list = new ArrayList<>();
        if (words.length == 0 ) return null;
        int[] hash = new int[26];
        for (int i = 0; i < words[0].length(); i++) {
            hash[words[0].charAt(i) - 'a']++;
        }
        for (int i = 1; i < words.length; i++) {
            int[] hashOtherstr = new int[26];
            for (int j = 0; j < words[i].length(); j++) {
                hashOtherstr[words[i].charAt(j) - 'a']++;
            }
            for (int k = 0; k < 26; k++) {
                hash[k] = Math.min(hash[k], hashOtherstr[k]);
            }
        }
        for (int i = 0; i < 26; i++) {
            while (hash[i] != 0) {
                hash[i]--;
                char ch = (char) (i + 'a');
                list.add(String.valueOf(ch));
            }
        }
        return list;
    }
}
